<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>修改密码 - 【少儿编程教学平台】</title>
    <link rel="icon" href="/img/logo.png">
    <link rel="stylesheet" type="text/css" href="/css/sUpdate.css">
    <script type="text/javascript" src="/js/jquery-3.4.1.min.js"></script>
    <script type="text/javascript" src="/js/layer/layer.js"></script>

</head>
<body>

<div class="headerpage"></div>

<div class="wrap">
    <div class="content">
        <div class="content-center">
            <div class="center-title">
                <h2>修改密码</h2>
            </div>
            <div class="center-content">
                <form>

                    <input type="hidden" name="sid" value="${sessionScope.SUserInfo.sid}"></input>
                    <label class="tou">旧密码</label><br>
                    <div class="kuang">
                        <input class="number" type="password" name="oldPassword" placeholder="请输入旧密码">

                    </div>
                    <label></label><br>

                    <label class="tou">新密码</label><br>
                    <div class="kuang">
                        <input class="number" type="password" name="newPassword" placeholder="请输入新密码，5-20位英文、数字、符号">

                    </div>
                    <label></label><br>


                    <label class="tou">确认新密码</label><br>
                    <div class="kuang">
                        <input class="number" type="password" name="cNewPassword" placeholder="确认新密码">
                    </div>
                    <label></label><br>


                    <button type="button" class="btn-update">确认修改密码</button>


                </form>
            </div>
        </div>
    </div>
</div>
<div class="footerpage"></div>

<script type="text/javascript">

    $(function () {
        $(".headerpage").load("header.jsp");
        $(".footerpage").load("footer.jsp");

        $("button").click(function () {

            var val1 = false, val2 = false;

            var reg = /^[0-9A-z$]{5,20}$/;

            if (reg.test($("[name='newPassword']")[0].value)) {
                if ($("[name='newPassword']")[0].value == $("[name='cNewPassword']")[0].value) {
                    if ($("[name='newPassword']")[0].value == "") {
                        layer.alert('新密码不能为空', {
                            skin: 'layui-layer-molv'
                            , closeBtn: 0, anim: 4
                        });
                    }
                    if ($("[name='oldPassword']")[0].value == "") {
                        layer.alert('原密码不能为空', {
                            skin: 'layui-layer-molv'
                            , closeBtn: 0, anim: 4
                        });
                    }
                    if ($("[name='cNewPassword']")[0].value == "") {
                        layer.alert('确认密码不能为空', {
                            skin: 'layui-layer-molv'
                            , closeBtn: 0, anim: 4
                        });
                    }
                    if ($("[name='cNewPassword']")[0].value != "" && $("[name='oldPassword']")[0].value != "" && $("[name='newPassword']")[0].value != "") {
                        if ($("[name='newPassword']")[0].value != $("[name='oldPassword']")[0].value) {

                            $.ajax({
                                type: 'post',
                                url: 'updatePassword.action',
                                data: {
                                    "oldPassword": $("[name='oldPassword']")[0].value,
                                    "newPassword": $("[name='newPassword']")[0].value,
                                    "sid": $("[name='sid']")[0].value

                                },

                                contentType: "application/x-www-form-urlencoded",

                                dataType: 'text',

                                success: function (data) {
                                    layer.alert(data, {
                                        skin: 'layui-layer-molv'
                                        , closeBtn: 0, anim: 4
                                    });


                                },

                                error: function (data) {
                                    console.log("请求失败");
                                }


                            })
                        } else {
                            layer.alert('新密码不能与原密码一致', {
                                skin: 'layui-layer-molv'
                                , closeBtn: 0, anim: 4
                            });
                        }
                    }
                } else {
                    layer.alert('两次密码不一致', {
                        skin: 'layui-layer-molv'
                        , closeBtn: 0, anim: 4
                    });
                }
            } else {
                $("[name='newPassword']").css("border", "1px solid red");
                layer.alert('请按照规则输入新密码', {
                    skin: 'layui-layer-molv'
                    , closeBtn: 0, anim: 4
                });
            }


        })
    })
</script>


</body>
</html>